package controllers

import (
	"encoding/json"
	"h5-parse/common"

	"github.com/astaxie/beego"
)

// H5_schemasController operations for H5_schemas
type H5_schemasController struct {
	beego.Controller
}

var _schemas_name = "schemas"

// URLMapping ...
func (c *H5_schemasController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Post ...
// @Title Create
// @Description create H5_schemas
// @Param	body		body 	models.H5_schemas	true		"body for H5_schemas content"
// @Success 201 {object} models.H5_schemas
// @Failure 403 body is empty
// @router / [post]
func (c *H5_schemasController) Post() {
	params := make(map[string]interface{})
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	_app_id := c.Ctx.Input.Header("X-Parse-Application-Id")
	_master_key := c.Ctx.Input.Header("X-Parse-Master-Key")
	_class_name := params["className"].(string)
	_data := params["data"].(interface{})
	_res, _err := common.H5_parse_post(_schemas_name, _class_name, _data, _app_id, _master_key)
	if _err != nil {
		c.Data["json"] = _err
	} else {
		c.Data["json"] = _res
	}

	c.ServeJSON()
}

// GetOne ...
// @Title GetOne
// @Description get H5_schemas by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.H5_schemas
// @Failure 403 :id is empty
// @router /:id [get]
func (c *H5_schemasController) GetOne() {
	_className := c.GetString("className")
	_object_id := c.GetString("objectID")
	_app_id := c.Ctx.Input.Header("X-Parse-Application-Id")
	_master_key := c.Ctx.Input.Header("X-Parse-Master-Key")

	_res, _err := common.H5_parse_get(_schemas_name, _className, _object_id, _app_id, _master_key)

	if _err != nil {
		c.Data["json"] = _err
	} else {
		c.Data["json"] = _res
	}
	c.ServeJSON()
}

// GetAll ...
// @Title GetAll
// @Description get H5_schemas
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.H5_schemas
// @Failure 403
// @router / [get]
func (c *H5_schemasController) GetAll() {
	_className := c.GetString("className")
	_app_id := c.Ctx.Input.Header("X-Parse-Application-Id")
	_master_key := c.Ctx.Input.Header("X-Parse-Master-Key")
	_object_id := ""
	_res, _err := common.H5_parse_get(_schemas_name, _className, _object_id, _app_id, _master_key)

	if _err != nil {
		c.Data["json"] = _err
	} else {
		c.Data["json"] = _res
	}
	c.ServeJSON()
}

// Put ...
// @Title Put
// @Description update the H5_schemas
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.H5_schemas	true		"body for H5_schemas content"
// @Success 200 {object} models.H5_schemas
// @Failure 403 :id is not int
// @router /:id [put]
func (c *H5_schemasController) Put() {
	params := make(map[string]interface{})
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	_app_id := c.Ctx.Input.Header("X-Parse-Application-Id")
	_master_key := c.Ctx.Input.Header("X-Parse-Master-Key")
	_className := params["className"].(string)
	_id := params["objectID"].(string)
	_data := params["data"].(interface{})
	_res, _err := common.H5_parse_put(_schemas_name, _className, _id, _data, _app_id, _master_key)

	if _err != nil {
		c.Data["json"] = _err
	} else {
		c.Data["json"] = _res
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the H5_schemas
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *H5_schemasController) Delete() {
	_className := c.GetString("className")
	_object_id := c.GetString("objectID")
	_app_id := c.Ctx.Input.Header("X-Parse-Application-Id")
	_master_key := c.Ctx.Input.Header("X-Parse-Master-Key")

	_res, _err := common.H5_parse_delete(_schemas_name, _className, _object_id, _app_id, _master_key)

	if _err != nil {
		c.Data["json"] = _err
	} else {
		c.Data["json"] = _res
	}
	c.ServeJSON()

}
